/**
 * fulihui.com Inc.
 * Copyright (c) 2015-2016 All Rights Reserved.
 */
package com.fulihui.integral.core.algorithm;

import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.SingleKeyTableShardingAlgorithm;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import java.util.Collection;

/**
 * t_is_account_info
 *
 * @author yunfeng.li
 * @version $Id: v 0.1 2017年04月12日 14:29 yunfeng.li Exp $
 */
public class TbISAccountInfoShardingAlgorithm implements SingleKeyTableShardingAlgorithm<String> {

    private final int TABLE_COUNT = 20;

    private final String DEFAULT_TABLE = "t_is_account_info";

    /**
     * sql 中 = 操作时，table的映射
     */
    public String doEqualSharding(Collection<String> tableNames, ShardingValue<String> shardingValue) {
        for (String each : tableNames) {
            Integer suffix = Math.abs(
                    new HashCodeBuilder().append(shardingValue.getValue()).hashCode() % TABLE_COUNT);
            if (each.endsWith("_" + suffix)) {
                return each;
            }
        }
        return DEFAULT_TABLE;
    }

    @Override
    public Collection<String> doInSharding(Collection<String> collection, ShardingValue<String> shardingValue) {
        return null;
    }

    @Override
    public Collection<String> doBetweenSharding(Collection<String> collection, ShardingValue<String> shardingValue) {
        return null;
    }
}
